/**
 * Created by vincent on 2017/1/6.
 */
function DateUtils() {
}

DateUtils.getTimeByElementId = function (id) {
    var dateString = document.getElementById(id).value;
    if (!StringUtils.isNlOrUndOrEmpty(dateString)) {
        return dateString;
    } else {
        return "";
    }
};

DateUtils.setTimeDefaultByElementId = function (id) {
    document.getElementById(id).value = "";
};

DateUtils.startTime = function (d) {
    if (!d) {
        d = new Date();
    }
    if (d instanceof Date) {
        d.setHours(0);
        d.setMinutes(0);
        d.setSeconds(0);
        return d;
    } else {
        var d_ = new Date(d);
        d_.setHours(0);
        d_.setMinutes(0);
        d_.setSeconds(0);
        return d_;
    }
}

DateUtils.endTime = function (d) {
    if (!d) {
        d = new Date();
    }
    if (d instanceof Date) {
        d.setHours(23);
        d.setMinutes(59);
        d.setSeconds(59);
        return d;
    } else {
        var d_ = new Date(d);
        d_.setHours(23);
        d_.setMinutes(59);
        d_.setSeconds(59);
        return d_;
    }
}

DateUtils.formatMsec = function (msec) {
    var minutes = parseInt(msec / (1000 * 60));
    var seconds = (msec % (1000 * 60)) / 1000;
    seconds = Math.round(seconds * 100) / 100;
    if (minutes == undefined || minutes == 0) {
        return seconds + " 秒 ";
    } else {
        return minutes + " 分 " + seconds + " 秒 ";
    }
};

//日期格式化
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}